home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / msm-2 / iconc.sit / tlocal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-19  |  6.9 KB  |  380 lines  |  [TEXT/MPS ]

  1. /*
  2.  *  tlocal.c -- functions needed for different systems.
  3.  */
  4.  
  5. #include "::h:gsupport.h"
  6.  
  7. /*
  8.  * The following code is operating-system dependent [@tlocal.01].
  9.  *  Routines needed by different systems.
  10.  */
  11.  
  12. #if PORT
  13. /* place to put anything system specific */
  14. Deliberate Syntax Error
  15. #endif                    /* PORT */
  16.  
  17. #if ATARI_ST
  18.  
  19. unsigned long _STACK = 10240;   /*   MNEED ALSO, PLEASE */
  20.  
  21. char *getenv();
  22.  
  23. char iconpath[80];        /* the path returned by findpath() */
  24. int pathset = 0;        /* not yet setup */
  25.  
  26. char *findpath();
  27. char *getpathenv();
  28.  
  29. /*
  30.  Pass a command to a shell.
  31.  Find what/where is the shell by the environment variable SHELL
  32.  */
  33. int system(cmd)
  34. char *cmd;
  35. {
  36.     char *shell, tail[128];
  37.     int rc;
  38.     
  39.     if ((shell = getenv("SHELL")) == NULL) {
  40.         fprintf(stderr,"SHELL not found.\n");
  41.         return -1;
  42.     }
  43.  
  44.     strcpy(tail + 1, cmd);
  45.     *tail = strlen(tail) - 1;
  46.  
  47.     rc = Pexec(shell, tail, NULL);
  48.     return (rc >= 0) ? rc : -1;
  49. }
  50.  
  51. /*
  52.  Spawn a process,
  53.  with: variable arg list, and path found by environment.
  54.  */
  55. int Forkvp(cmd, argv)
  56. char *cmd, **argv;
  57. {
  58.     char file[80];
  59.     int n;
  60.     extern char *patharg;
  61.  
  62.     if (filefound(NULL, cmd) == 0) {
  63.         if (!pathset) {
  64.             if (patharg) {
  65.                 strcpy(iconpath, patharg);
  66.                 if ((n = strlen(iconpath)) > 0
  67.                         && iconpath[n-1] != '\\')
  68.                     strcat(iconpath, "\\");
  69.             } else {
  70.                 if (findpath(cmd) == NULL)
  71.                     return -1;
  72.             }
  73.             pathset = 1;
  74.         }
  75.         strcpy(file, iconpath);
  76.         strcat(file, cmd);
  77.     } else
  78.         strcpy(file, cmd);
  79.  
  80.     return Forkv(file, argv);    
  81. }
  82.  
  83. /*
  84.  Spawn a process with variable arg list.
  85.  */
  86. int Forkv(cmd, argv)
  87. char *cmd, **argv;
  88. {
  89.     char tail[128];
  90.     int i;
  91.  
  92.     *tail = tail[1] = '\0';
  93.     for (i = 1; argv[i]; i++) {
  94.         strcat(tail," ");
  95.         strcat(tail,argv[i]);
  96.     }
  97.     *tail = strlen(tail) - 1;
  98.  
  99.     return Pexec(cmd, tail, NULL);    
  100. }
  101.  
  102. /*
  103.  findpath() assumes that the name passed to it is complete.
  104.  (i.e. with the proper extension...)
  105.  */  
  106. char *findpath(prog)
  107. char *prog;
  108. {
  109.     char *ppath, *fpath;
  110.     int error, drvnum, drvmap, drvmask;
  111. /*    
  112.  First : check if ICON environment variable exist...
  113.  (because it may be a lot faster than check the whole PATH...)
  114.  */
  115.     if ((ppath = getenv("ICON")) != NULL) {
  116.         strcpy(iconpath, ppath);
  117.         if ((error = strlen(iconpath)) > 0) {
  118.             if (iconpath[error - 1] != '\\')
  119.                 strcat(iconpath, "\\");
  120.             if (filefound(iconpath, prog) != 0)
  121.                 return iconpath;
  122.         }
  123.     }
  124. /*    
  125.  Second : check with PATH...
  126.  */
  127.  
  128.     if ((ppath = getenv("PATH")) != NULL) {
  129.         error = 0;
  130.         while ((fpath = getpathenv(ppath, error)) != NULL) {
  131.             if (filefound(fpath, prog) != 0) {
  132.                 strcpy(iconpath, fpath);
  133.                 return iconpath;
  134.             }
  135.             error++;
  136.         }
  137.     }
  138.  
  139. /*
  140.  Not found here or with the ICON or PATH environment variable.
  141.  Check all the mounted disk drives root directory, in alphabetical order.
  142.  */
  143.     strcpy(iconpath, "a:\\");
  144.     drvmap = bios(10);    /* Drvmap() */ 
  145.     for (drvnum = 0,    drvmask = 1;
  146.          drvnum < 16;
  147.          drvnum++,        drvmask <<= 1, iconpath[0]++)
  148.         if ((drvmask & drvmap) && (filefound(iconpath, prog) != 0))
  149.                 return iconpath;
  150.  
  151. /*
  152.  Not found...
  153.  */
  154.     return NULL;
  155. }
  156.  
  157. int Pexec(file, tail, env) char *file, *tail, *env;
  158. {
  159.     int rc;
  160.  
  161.     if ((rc = gemdos(0x4b,0,file,tail, env)) != 0) {
  162.         switch (rc) {
  163.             case -33:
  164.                 fprintf(stderr,
  165.                 "Fork failed: file <%s> not found.\n", file);
  166.                 break;
  167.             case -39:
  168.                 fprintf(stderr,
  169.                 "Fork failed: <%s> Not enough memory.\n", file);
  170.                 break;
  171.             case -66:
  172.                 fprintf(stderr,
  173.                 "Fork failed: <%s> Bad load format.\n", file);
  174.                 break;
  175.             default:
  176.         }
  177.     }
  178.     return rc;
  179. }
  180.  
  181. /*
  182.  getpathenv() find the nth path entry in the env var PATH.
  183.  append a backslash to it, if necessary; strcat ready.
  184.  */
  185.  
  186. char *getpathenv(path, n) char *path; int n;
  187. {
  188.     char *Path2;
  189.     char *sep = " ,;";        /* path separator in PATH */
  190.     static char local[128];
  191.     int i, j;
  192.  
  193.     for (i = 0; i < n; i++) {
  194.         Path2 = strpbrk(path, sep);
  195.         if (Path2 == NULL)
  196.             break;
  197.         path = Path2 + 1;    /* Past the sep */
  198.         if ((j = strspn(path, sep)) > 0)
  199.             path += j;
  200.         
  201.     }
  202.     if (n == i) {
  203.         strcpy(local, path);
  204.         Path2 = strpbrk(local, sep);
  205.         if (Path2 != NULL) {
  206.             *Path2++ = '\\';
  207.             *Path2 = '\0';
  208.         } else
  209.             strcat(local, "\\");
  210.         if (strlen(local) == 1)
  211.             local[0] = '\0';
  212.         return &local[0];
  213.     }
  214.     return NULL;
  215. }
  216.     
  217. /*
  218.  test if a file exists.
  219.  */
  220. int filefound(path, file) char *path, *file;
  221. {
  222.     char filepath[80];
  223.  
  224.     if ((path != NULL) && (path[0] != '\0')) {
  225.         strcpy(filepath, path);
  226.         strcat(filepath, file);
  227.     } else
  228.         strcpy(filepath, file);
  229.  
  230.     if (gemdos(0x43, filepath, 0, 0) >= 0)
  231.         return 1;
  232.     return 0;
  233. }
  234.  
  235. /*
  236.  * This function returns a pointer to a static character string containing
  237.  * the environment value assigned to the variable passed. (Fonorow/Nowlin)
  238.  */
  239.  
  240. #define  BASEPAGE   _basepage
  241. #define  SETLEN     256
  242.  
  243. char    *getenv(var)
  244. char    *var;
  245. {
  246.     extern
  247.     long    *BASEPAGE;
  248.  
  249.     register
  250.     int    varlen;
  251.  
  252.     register
  253.     char    *lenv;
  254.  
  255.     static
  256.     char    val[SETLEN];
  257.  
  258. /*    calculate the variable length */
  259.     varlen = strlen(var);
  260.  
  261. /*    check all the variables in the environment */
  262.     for (lenv = (char *) BASEPAGE[11]; *lenv; lenv += strlen(lenv) + 1) {
  263.  
  264. /*        if this variable matches the variable name passed exactly */
  265.         if (*(lenv+varlen) == '=' &&
  266.             strncmp(var,lenv,varlen) == 0) {
  267.  
  268. /*            save the value and return a pointer to it */
  269.             strcpy(val,lenv+varlen+1);
  270.  
  271.             return val;
  272.         }
  273.     }
  274.  
  275. /*    the variable wasn't found so return null */
  276.     return (char *) 0;
  277. }
  278. #endif                    /* ATARI_ST */
  279.  
  280. #if MACINTOSH
  281. #if MPW
  282. /*
  283.  * These are stubs for several routines defined in the runtine
  284.  *  library that aren't necessary in MPW tools.  These routines are 
  285.  *  referenced by the Standard C Library I/O functions, but are never called.
  286.  *  Because they are referenced, the linker can't remove them.  The stubs in
  287.  *  this file provide dummy routines which are never called, but reduce the
  288.  *  size of the tool.
  289.  */
  290.  
  291.  
  292. /* Console Driver
  293.  
  294.    These drivers provide I/O to the screen (or a specified port) in
  295.    applications.  They aren't necessary in tools.  
  296. */
  297.  
  298. _coFAccess() {}
  299. _coClose() {}
  300. _coRead() {}
  301. _coWrite() {}
  302. _coIoctl() {}
  303. _coExit() {}
  304.  
  305.  
  306. /* File System Driver
  307.  
  308.    Tools use the file system drivers linked with the MPW Shell.
  309. */
  310.  
  311. _fsFAccess() {}
  312. _fsClose() {}
  313. _fsRead() {}
  314. _fsWrite() {}
  315. _fsIoctl() {}
  316. _fsExit() {}
  317.  
  318.  
  319. /* System Driver
  320.  
  321.    Tools use the system drivers linked with the MPW Shell.
  322. */
  323.  
  324. _syFAccess() {}
  325. _syClose() {}
  326. _syRead() {}
  327. _syWrite() {}
  328. _syIoctl() {}
  329. _syExit() {}
  330.  
  331.  
  332. /* Floating Point Conversion Routines
  333.  
  334.    These routines, called by printf, are only necessary if floating point
  335.    formatting is used.
  336. */
  337.  
  338. ecvt() {}
  339. fcvt() {}
  340. #endif                    /* MPW */
  341. #endif                    /* MACINTOSH */
  342.  
  343. #if MSDOS
  344.  
  345. #if MICROSOFT
  346.  
  347. pointer xmalloc(n)
  348.    long n;
  349.    {
  350.    return calloc((size_t)n,sizeof(char));
  351.    }
  352. #endif                    /* MICROSOFT */
  353.  
  354. #if MICROSOFT || LATTICE
  355. int _stack = (8 * 1024);
  356. #endif                    /* MICROSOFT || LATTICE */
  357.  
  358. #if TURBO
  359. extern unsigned _stklen = 8192;
  360. #endif                    /* TURBO */
  361. #endif                    /* MSDOS */
  362.  
  363. #if MVS || VM
  364. #endif                    /* MVS || VM */
  365.  
  366. #if OS2
  367. #endif                    /* OS2 */
  368.  
  369. #if UNIX
  370. #endif                    /* UNIX */
  371.  
  372. #if VMS
  373. #endif                    /* VMS */
  374.  
  375. /*
  376.  * End of operating-system specific code.
  377.  */
  378.  
  379. char *tjunk;            /* avoid empty module */
  380.